package com.zhengshang.api.controller;

import com.alibaba.fastjson.util.TypeUtils;
import com.zhengshang.api.domain.AppBanner;
import com.zhengshang.api.domain.AppPhoneArea;
import com.zhengshang.common.core.domain.AjaxResult;
import com.zhengshang.common.utils.StringUtils;
import com.zhengshang.system.domain.SysConfig;
import com.zhengshang.system.domain.SysNotice;
import com.zhengshang.system.service.ISysConfigService;
import com.zhengshang.system.service.ISysNoticeService;
import com.zhengshang.third.phone.PhoneAreaUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zyf
 * @date: 2018/11/9 15:43
 * @description: 基础服务模块接口, 不需要登录鉴权的接口
 */
@Api(tags = "基础模块-公共接口")
@Controller
@RequestMapping("/common")
public class AppCommonController {

    @Autowired
    private ISysNoticeService sysNoticeService;

    @Autowired
    private PhoneAreaUtils phoneAreaUtils;

    @Autowired
    private ISysConfigService sysConfigService;


    /**
     * 获取服务协议
     */
    @GetMapping(value = "/html/{code}")
    public String getHtml(ModelMap map, @PathVariable String code) {
        Map html = new HashMap();
        SysNotice notice = sysNoticeService.selectNoticeByNoticeType(code);
        html.put("htmlTitle", notice.getNoticeTitle());
        html.put("noticeContent", notice.getNoticeContent());
        map.addAttribute("html", html);
        return "html";
    }

    /**
     * 获取服务协议
     */
    @GetMapping(value = "/html/msgNotice/{noticeId}")
    public String getNoticeHtml(ModelMap map, @PathVariable Long noticeId) {
        Map html = new HashMap();
        SysNotice notice = sysNoticeService.selectNoticeById(noticeId);
        html.put("htmlTitle", notice.getNoticeTitle());
        html.put("noticeContent", notice.getNoticeContent());
        map.addAttribute("html", html);
        return "html";
    }

    /**
     * 获取公告列表
     */
    @GetMapping(value = "/msgNotice/list")
    @ResponseBody
    public AjaxResult getMsgNoticeList() {
        List<Map<String, Object>> noticeList = sysNoticeService.queryMsgNoticeList("0", 10);
        String domainUrl = sysConfigService.selectConfigByKey("sys.domain.name");
        for (Map<String, Object> map : noticeList) {
            Long noticeId = TypeUtils.castToLong(map.get("noticeId"));
            String detailUrl = domainUrl + "api/common/html/msgNotice/" + noticeId;
            map.put("detailUrl", detailUrl);
        }
        return AjaxResult.success(noticeList);
    }

    /**
     * 获取公告列表
     */
    @GetMapping(value = "/msgNotice/pageList")
    @ResponseBody
    public AjaxResult getMsgNoticePageList(@RequestParam Integer pageNum, @RequestParam(required = false) Integer pageSize) {
        if (pageNum == null || pageNum.intValue() <= 0 || (pageSize != null && pageSize.intValue() < 1)) {
            return AjaxResult.error("参数错误");
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        if (pageSize.intValue() > 100) {
            pageSize = 100;
        }
        String domainUrl = sysConfigService.selectConfigByKey("sys.domain.name");
        SysNotice sysNotice = new SysNotice();
        sysNotice.setStatus("0");
        sysNotice.getParams().put("pageNum", pageNum);
        sysNotice.getParams().put("pageSize", pageSize);
        List<SysNotice> noticeList = sysNoticeService.selectMsgNoticeList(sysNotice);
        for (SysNotice notice : noticeList) {
            Long noticeId = notice.getNoticeId();
            String detailUrl = domainUrl + "api/common/html/msgNotice/" + noticeId;
            notice.setDetailUrl(detailUrl);
        }
        return AjaxResult.success(noticeList);
    }

    /**
     * 获取公告列表
     */
    @GetMapping(value = "/msgNotice/detail")
    @ResponseBody
    public AjaxResult getMsgNoticeDetail(Long noticeId) {
        Map<String, Object> detail = sysNoticeService.getMsgNoticeDetail(noticeId);
        return AjaxResult.success(detail);
    }

    /**
     * 获取手机号信息
     */
    @GetMapping(value = "/getPhoneInfo")
    @ResponseBody
    public AjaxResult getPhoneInfo(String phone) {
        AppPhoneArea phoneInfo = phoneAreaUtils.getPhoneInfo(phone);
        if (StringUtils.isNull(phoneInfo)) {
            return AjaxResult.success(new HashMap<>());
        }
        return AjaxResult.success(phoneInfo);
    }

}
